package wujinye.com.controller.article;

import wujinye.com.controller.BaseServlet;
import wujinye.com.model.Article;
import wujinye.com.service.ArticleService;
import wujinye.com.service.impl.ArticleServiceImpl;
import wujinye.com.utils.StrUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

@WebServlet("/article/*")
public class ArticleServlet extends BaseServlet {

    ArticleService articleService = new ArticleServiceImpl();

    /**
     * 跳转article.jsp的请求,浏览全部文章
     *
     * @param req
     * @param resp
     */
    public void articleList(HttpServletRequest req, HttpServletResponse resp) {
        try {
            String currentPage = req.getParameter("page");
            String search = req.getParameter("search");
            int page = 1;
            if (StrUtil.checkString(currentPage)) {
                page = StrUtil.stringToInteger(currentPage);
            }
            if (StrUtil.checkString(search)) {
                String encode = URLEncoder.encode(search, "UTF-8");
                resp.sendRedirect(req.getContextPath() + "/view/showArticle?page=" + page + "&search=" + encode);
            } else {
                resp.sendRedirect(req.getContextPath() + "/view/showArticle?page=" + page);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 取消收藏文章
     *
     * @param req
     * @param resp
     */
    public void removeCollect(HttpServletRequest req, HttpServletResponse resp) {
        try {
            String aId = req.getParameter("aId");
            String userId = req.getSession().getAttribute("userId").toString();
            if (StrUtil.checkString(aId) && StrUtil.checkString(userId)) {
                articleService.removeCollect(StrUtil.stringToInteger(userId), StrUtil.stringToInteger(aId));
                resp.getWriter().write("success");
            } else {
                resp.getWriter().write("取消收藏失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 添加收藏文章
     *
     * @param req
     * @param resp
     */
    public void addCollect(HttpServletRequest req, HttpServletResponse resp) {
        try {
            String aId = req.getParameter("aId");
            String userId = req.getSession().getAttribute("userId").toString();
            if (StrUtil.checkString(aId) && StrUtil.checkString(userId)) {
                String collect = articleService.isCollect(StrUtil.stringToInteger(userId), StrUtil.stringToInteger(aId));
                if ("0".equals(collect)) {
                    articleService.addCollect(StrUtil.stringToInteger(userId), StrUtil.stringToInteger(aId));
                    resp.getWriter().write("success");
                } else {
                    resp.getWriter().write("添加收藏失败");
                }
            } else {
                resp.getWriter().write("添加收藏失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 发布文章
     *
     * @param req
     * @param resp
     */
    public void addArticle(HttpServletRequest req, HttpServletResponse resp) {
        String title = req.getParameter("title");
        String content = req.getParameter("content");
        String userId = req.getSession().getAttribute("userId").toString();
        try {
            if (StrUtil.checkString(title)) {
                articleService.addArticle(StrUtil.stringToInteger(userId), title, content);
                resp.sendRedirect(req.getContextPath() + "/view/showArticle");
            } else {
                req.getSession().setAttribute("releasemsg", "标题不能为空，发布失败！！！");
                resp.sendRedirect(req.getContextPath() + "/view/writeArticle");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 展示用户收藏的文章列表
     *
     * @param req
     * @param resp
     */
    public void myArticleList(HttpServletRequest req, HttpServletResponse resp) {
        try {
            String currentPage = req.getParameter("page");
            String search = req.getParameter("search");
            int page = 1;
            if (StrUtil.checkString(currentPage)) {
                page = StrUtil.stringToInteger(currentPage);
            }
            if (StrUtil.checkString(search)) {
                String encode = URLEncoder.encode(search, "UTF-8");
                resp.sendRedirect(req.getContextPath() + "/view/showArticle?page=" + page + "&search=" + encode + "&uId=1");
            } else {
                resp.sendRedirect(req.getContextPath() + "/view/showArticle?page=" + page + "&uId=1");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
